⚡️ Speed up function _cached_joined by 15,102%
#426
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 15,102% (151.02x) speedup for
_cached_joinedincode_to_optimize/code_directories/simple_tracer_e2e/workload.py⏱️ Runtime :
75.9 milliseconds→500 microseconds(best of288runs)📝 Explanation and details
Here is a faster version of your program. The current code converts each integer to a string individually using map and then joins them. This can be improved by.
" ".join()with map is already very efficient.But keeping the same signature and caching behavior, here's a further optimized version using a single global list to store precomputed results, which is faster than lru_cache with repeated access patterns (and fully thread-safe for read-only access).
This version will be significantly faster for numbers from 0 to 1000, which matches the intent of the original lru_cache(maxsize=1001), and is just as correct for larger (non-cached) values. The memory overhead is similar to lru_cache at its maximum, but with much lower per-call computational cost for the most common cases.
Comments are unchanged unless relevant to the modification.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_cached_joined-mccvfhe2and push.